local life $life

local ave_panels $ave_panels

local r $r

local graphs 0

local KW_per_panel=$KW_per_panel
* https://news.energysage.com/how-many-solar-panels-do-i-need/


local model_type CollPercPolD

local net_metering 1

local over_dist 0

**************************************************
************** Merging Code***********************
**************************************************




clear

insheet using `"../StructuralCode/Outputs/M_t`model_type'nm`net_metering'Base.csv"' // total panels

rename v1 M_t_pred

save ../Data/CleanedData/M_t`model_type'nm`net_metering'Base, replace

clear

insheet using `"../StructuralCode/Outputs/HH_tract.csv"' // HH's (google)

rename v1 HH_tract

save ../Data/CleanedData/HH_tract, replace

clear

insheet using `"../StructuralCode/Outputs/BI`model_type'nm`net_metering'Base.csv"' // total panels

rename v1 BI_t_pred

save ../Data/CleanedData/BI_t`model_type'nm`net_metering'Base, replace

clear

insheet using `"../StructuralCode/Outputs/Fisc`model_type'nm`net_metering'Base.csv"' // total panels

rename v1 Fisc_t_pred

save ../Data/CleanedData/Fisc_t`model_type'nm`net_metering'Base, replace

clear

insheet using `"../StructuralCode/Outputs/D`model_type'nm`net_metering'Base.csv"' // total panels

rename v1 D_pred

save ../Data/CleanedData/D`model_type'nm`net_metering'Base, replace




sum

use ../Data/CleanedData/mergedData, clear // this contains all the stuff from the data


sum

merge 1:1 _n using ../Data/CleanedData/M_t`model_type'nm`net_metering'Base, nogen

merge 1:1 _n using ../Data/CleanedData/BI_t`model_type'nm`net_metering'Base, nogen

merge 1:1 _n using ../Data/CleanedData/Fisc_t`model_type'nm`net_metering'Base, nogen

merge 1:1 _n using ../Data/CleanedData/HH_tract, nogen




save ../Data/CleanedData/MergedData2, replace

use ../Data/CleanedData/MergedData2, clear


**************************************************
**************Done Merging Code*******************
**************************************************

gen Prob_b_sim=BI_t_pred/HH_tract

gen Prob_b_data=BI_t/HH_tract

gen l_Prob_b_sim=log(Prob_b_sim)
gen l_Prob_b_data=log(Prob_b_data)

sum l_Prob*

gen panels_per_data=M_t/BI_t

gen panels_per_sim=M_t_pred/BI_t_pred

reg panels_per_data tot_ben1000

reg panels_per_sim tot_ben1000





corr l_Prob_b_sim l_Prob_b_data




twoway (lpoly l_Prob_b_sim CollPerc_t  [aw=HH_tract], bw(.01) )(lpoly l_Prob_b_data CollPerc_t  [aw=HH_tract], bw(.01)  ) 


graph export StataGraphs/fine_lpoly_fit_CollPerc_t.png, as(png) replace	

forvalues cut=-.25(.25).75 {
	sum  l_Prob_b_sim l_Prob_b_data CollPerc_t  HH_tract if CollPerc_t>(`cut'-.25) & CollPerc_t<`cut'
	
}




	sum  l_Prob_b_sim l_Prob_b_data tot_ben  HH_tract if tot_ben<1804.043
	sum  l_Prob_b_sim l_Prob_b_data tot_ben  HH_tract if tot_ben>=1804.043
	
	
	
	sum  l_Prob_b_sim l_Prob_b_data
		
		


*************

foreach var in tot_ben Pol_t CollPerc_t {
sum `var'

local bw=r(sd)

local var_title Monetary Benefit

if "`var'"=="Pol_t" {
	local var_title Percent Dem
}

if "`var'"=="CollPerc_t" {
	local var_title Percent College
}

twoway (lpoly l_Prob_b_sim `var'  [aw=HH_tract], bw(`bw') lwidth(thick) lcolor(blue) lpattern(solid)  ) ///
(lpoly l_Prob_b_data `var'  [aw=HH_tract], bw(`bw') lwidth(thick) lcolor(red) lpattern(dash)  ) , ///
graphregion(color(white))  xlabel(,labsize(medlarge)) ylabel(,labsize(medlarge)) ytitle("Log Install Perc",size(huge)) xtitle("`var_title'",size(huge)) legend(order(1 "Predicted " 2 "Actual" ) size(huge)) // legend(off) // legend(order(1 "Predicted " 2 "Actual" ))

graph export StataGraphs/lpoly_fit_`var'.png, as(png) replace	



}

